<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column
                label="序号"
                width="120"
                type="index"
              />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="doEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="doDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="params.page"
                :page-sizes="[2, 4, 6, 8]"
                :page-size="params.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit?'编辑角色':'新增角色'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="doClose"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="submit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { addRole, delRole, getRoleList, updataeRole } from '@/api/settings'
export default {
  data() {
    return {
      params: {
        page: 1,
        pagesize: 5
      },
      total: 0,
      roleList: [],
      showDialog: false,
      isEdit: false, // 默认新增
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadRoleList()
  },
  methods: {
    // 清空表单
    doClose() {
      this.$refs.roleForm.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 提交
    submit() {
      this.$refs.roleForm.validate(valid => {
        if (!valid) return
        this.isEdit ? this.editRole() : this.doAdd()
        this.showDialog = false
      })
    },
    // 添加角色
    async doAdd() {
      const { data } = await addRole(this.roleForm)
      console.log(data)
      this.total++
      this.params.page = Math.ceil(this.total / this.params.pagesize)
      this.$message.success(data.message)
      this.loadRoleList()
    },
    // 编辑角色
    doEdit(row) {
      this.showDialog = true
      this.isEdit = true
      this.roleForm = { ...row }
    },
    // 编辑完成
    async editRole() {
      try {
        const { data } = await updataeRole(this.roleForm)
        this.$message.success(data.message)
        this.loadRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    // 删除角色
    async doDel(id) {
      const { data } = await delRole(id)
      if (this.roleList.length === 1) {
        this.params.page--
        if (this.params.page <= 0) {
          this.params.page = 1
        }
      }
      this.$message.success(data.message)
      this.loadRoleList()
    },
    // 获取角色列表 渲染
    async loadRoleList() {
      const { data } = await getRoleList(this.params)
      console.log(data)
      this.roleList = data.data.rows
      this.total = data.data.total
    },
    // 切换每页显示条数
    handleSizeChange(val) {
      console.log(val)
      this.params.pagesize = val
      this.loadRoleList()
    },
    // 切换页码
    handleCurrentChange(val) {
      console.log(val)
      this.params.page = val
      this.loadRoleList()
    }
  }
}
</script>
